﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace GenCode
{
    public partial class frmMain : DevExpress.XtraEditors.XtraForm
    {
        string database = "";
        string table = "";
        string name_space = "";
        DataTable m_DataSource = new DataTable();
        public frmMain()
        {
            Form1 frm = new Form1();
            frm.ShowDialog();
            if (frm.yes==false)
            {
                Application.Exit();
                return;
            }
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
           try
           {
              this.Text = "Gencode Rebuil (canhhungit@gmail.com)";
              //GenCodeDAL.ShowDatabase(cbDatabase);
              //cbDatabase.SelectedIndex = 0;
              //txtNameSpace.Text = Properties.Settings.Default["NameSpace"].ToString();
           }
           catch(Exception ex)
           {
              XtraMessageBox.Show(ex.Message);
           }
        }

        private void btnGenCode_Click(object sender, EventArgs e)
        {
            try
            {
                name_space = txtNameSpace.Text.Trim();
                if (txtNameSpace.Text == "")
                {
                    XtraMessageBox.Show("Nhập tên namespace");
                    return;
                }
                switch (TabControl1.SelectedTabPage.Name)
                {
                    case "tabInfo":
                        txtCodeInfo.Text = "";
                        txtCodeInfo.Text = GenCodeDAL.CodeInfo(database, table, name_space);

                        //System.IO.File.WriteAllText(txtDuongDan.Text+ @"\WriteText.cs", txtCodeInfo.Text, Encoding.Unicode);
                        break;
                    case "tabDAL":
                        txtCodeDAL.Text = "";
                        txtCodeDAL.Text = GenCodeDAL.CodeDAL(database, table, name_space);
                        break;
                    case "tabBUS1":
                        txtCodeBUS1.Text = "";
                        txtCodeBUS1.Text = GenCodeDAL.CodeBUSsingle(database, table, name_space, cbColumnValue.EditValue.ToString(), cbCheckColumn.EditValue.ToString(), cbCheckTable.EditValue.ToString());
                        break;
                    case "tabProvider":
                        txtProvider.Text = "";
                        txtProvider.Text = GenCodeDAL.Provider(name_space);
                        break;
                    case "tabGlobal":
                        txtGlobal.Text = "";
                        txtGlobal.Text = GenCodeDAL.Global(database, name_space);
                        if (ck.Checked)
                        {
                            if (System.IO.File.Exists(txtDuongDan.Text + @"\Global.cs"))
                            {
                                if (XtraMessageBox.Show("Bạn có muốn ghi đè file này không?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                                {
                                    System.IO.File.WriteAllText(txtDuongDan.Text + @"\Global.cs", txtGlobal.Text, Encoding.Unicode);
                                }
                            }
                            else
                                System.IO.File.WriteAllText(txtDuongDan.Text + @"\Global.cs", txtGlobal.Text, Encoding.Unicode);

                        }
                        break;
                    //Algorithm tabFormDesigner
                    case "tabAlgorithm":
                        txtAlgorithm.Text = "";
                        txtAlgorithm.Text = GenCodeDAL.Algorithm(name_space);

                        if (ck.Checked)
                        {
                            if (System.IO.File.Exists(txtDuongDan.Text + @"\Algorithm.cs"))
                            {
                                if (XtraMessageBox.Show("Bạn có muốn ghi đè file này không?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                                {
                                    System.IO.File.WriteAllText(txtDuongDan.Text + @"\Algorithm.cs", txtAlgorithm.Text, Encoding.Unicode);
                                }
                            }
                            else
                                System.IO.File.WriteAllText(txtDuongDan.Text + @"\Algorithm.cs", txtAlgorithm.Text, Encoding.Unicode);

                        }
                        break;
                    case "tabFormDesigner":
                       
                            txtFormDesigner.Text = "";
                            txtFormDesigner.Text = FormDesigner.CodeFormDesigner(database, table, name_space, ref m_DataSource);
                            if (ck.Checked)
                            {
                                if (System.IO.File.Exists(txtDuongDan.Text + @"\frm" + table + ".designer.cs"))
                                {
                                    if (XtraMessageBox.Show("Bạn có muốn ghi đè file này không?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                                    {
                                        System.IO.File.WriteAllText(txtDuongDan.Text + @"\frm" + table + ".designer.cs", txtFormDesigner.Text, Encoding.Unicode);
                                    }
                                }
                                else
                                    System.IO.File.WriteAllText(txtDuongDan.Text + @"\frm" + table + ".designer.cs", txtFormDesigner.Text, Encoding.Unicode);

                            }
                        break;
                    case "tabCodeForm":
                        txtCodeForm.Text = "";
                        txtCodeForm.Text = FormDesigner.CodeForm(table, name_space);
                        if (ck.Checked)
                        {
                            if (System.IO.File.Exists(txtDuongDan.Text + @"\frm" + table + ".cs"))
                            {
                                if (XtraMessageBox.Show("Bạn có muốn ghi đè file này không?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                                {
                                    System.IO.File.WriteAllText(txtDuongDan.Text + @"\frm" + table + ".cs", txtCodeForm.Text, Encoding.Unicode);
                                }
                            }
                            else
                                System.IO.File.WriteAllText(txtDuongDan.Text + @"\frm" + table + ".cs", txtCodeForm.Text, Encoding.Unicode);

                        }
                        break;
                    case "tabPageBase":
                        txtPageBase.Text = "";
                        txtPageBase.Text = GenCodeDAL.PageBase(name_space);
                        if (ck.Checked)
                        {
                            if (System.IO.File.Exists(txtDuongDan.Text + @"\PageBase.cs"))
                            {
                                if (XtraMessageBox.Show("Bạn có muốn ghi đè file này không?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                                {
                                    System.IO.File.WriteAllText(txtDuongDan.Text + @"\PageBase.cs", txtPageBase.Text, Encoding.Unicode);
                                }
                            }
                            else
                                System.IO.File.WriteAllText(txtDuongDan.Text + @"\PageBase.cs", txtPageBase.Text, Encoding.Unicode);

                        } break;
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
            }
        }
        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
           try
           {

              Properties.Settings.Default["NameSpace"] = txtNameSpace.Text.Trim();
              Properties.Settings.Default["Setting"] = txtDuongDan.Text.Trim();
              Properties.Settings.Default.Save();
              Application.Exit();
           }
           catch(Exception ex)
           {
              XtraMessageBox.Show(ex.Message);
           }
        }

        private void cbDatabase_SelectedIndexChanged(object sender, EventArgs e)
        {
           try
           {
              database = cbDatabase.EditValue.ToString();
              GenCodeDAL.ShowTableName(cbTable,database);
              cbTable.SelectedIndex = 0;
              GenCodeDAL.ShowTableName(cbCheckTable,database);
              cbCheckTable.SelectedIndex = 0;
           }
           catch(Exception ex)
           {
              XtraMessageBox.Show(ex.Message);
           }
           
        }

        private void cbTable_SelectedIndexChanged(object sender, EventArgs e)
        {
           try
           {
              table = cbTable.EditValue.ToString();
              GenCodeDAL.ShowColumnName(cbColumnValue,database,table);
              cbColumnValue.SelectedIndex = 0;
              GenCodeDAL.ShowColumnName(cbCheckColumn,database,table);
              cbCheckColumn.SelectedIndex = 0;
              FormDesigner.FillRepository(repositoryItem);
              FormDesigner.FillGrid(database,table,ref m_DataSource,gridControl1);
           }
           catch(Exception ex){
              XtraMessageBox.Show(ex.Message);
           }

        }

        private void TabControl1_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)
        {
            //if (TabControl1.SelectedTabPage.Name == "tabFormDesigner")
            //{
            //    FormDesigner.FillGrid(database, table, ref m_DataSource, gridControl1);
            //}
        }

        private void frmMain_Shown(object sender, EventArgs e)
        {
            try
            {
               // this.Text = "Gencode Rebuil (canhhungit@gmail.com)";
                GenCodeDAL.ShowDatabase(cbDatabase);
                cbDatabase.SelectedIndex = 0;
                txtNameSpace.Text = Properties.Settings.Default["NameSpace"].ToString();
                txtDuongDan.Text = Properties.Settings.Default["Setting"].ToString();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
            }
        }

        private void btnDuyet_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog op = new FolderBrowserDialog();
            if (op.ShowDialog()==DialogResult.OK)
            {
                txtDuongDan.Text = op.SelectedPath;
            }
        }
    }
}
